import 'dart:developer';

import 'package:get/get.dart';
import 'package:sports_new/entity/expert/plan_pack/plan_pack_entity.dart';
import 'package:sports_new/server/api/expert_api.dart';

import '../../../entity/pay/recharge_entity.dart';
import '../../../server/api/pay_api.dart';
import '../../../server/api/pay_api.dart';
import '../../../utils/user.dart';
import '../../../utils/utils.dart';
import '../../../widgets/pay_bottom_sheet.dart';

class PlanPackDetailLogic extends GetxController{
  int? packId = Get.arguments;
  PlanPackEntity? data;

  @override
  void onReady() {
    getData();
    super.onReady();
  }

  Future getData() async{
    data = await ExpertApi.getPlanPackDetail(packId??0);
    update();
  }

  Future toPay() async{
    final price = data?.price;
    if (price == null) {
      return;
    }
    final gold = double.tryParse(price);
    if (gold == null) {
      return;
    }
    final PayResult? result = await PayBottomSheet.show(
      payInfo: PayInfo(
        price: price,
        payScene: PayScene.planPack,
        buyId: "${data?.packageId}"
      )
    );
    if(result != null && result.toPay == PayStatus.goldPay){
      log("--------goldPay--------");
      var s = await PayApi.planPackOrder(data?.packageId);
      if(s == true) {
        Utils.onEvent('pay_done');
        await getData();
      }else{
        Utils.onEvent('fatcxq_zfyd_qxzf');
      }
    }else if(result?.toPay == PayStatus.directPay){
      log("--------directPay--------");
      Utils.onEvent('fatcxq_zfyd_qrzf');
      await getData();
    }else{
      Utils.onEvent('fatcxq_zfyd_qxzf');
    }
    User.fetchUserInfo(fetchFocus: false);
  }
}